const app = getApp();
const util = require('../../../utils/util.js');
const lbbService = require('../../../pages/service/service.js');
const lbbTaskService = require('../service/task-service.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        invoiceList:[],
        yearList:[
            new Date().getYear()+1900,
            new Date().getYear()+1900-1,
            new Date().getYear()+1900-2,
            new Date().getYear()+1900-3,
            new Date().getYear()+1900-4,
            new Date().getYear()+1900-5,
            new Date().getYear()+1900-6,
            new Date().getYear()+1900-7,
            new Date().getYear()+1900-8,
            new Date().getYear()+1900-9,
        ],
        monthList:['全部月份','01','02','03','04','05','06','07','08','09','10','11','12'],
        cityShow: '0',
        invoiceName: '全部',
        userId:-1,
        selectYear:new Date().getYear()+1900,
        selectMonth:'全部月份',
        invoice_animationData: {},
        year_animationData: {}, 
        month_animationData: {},
        pageNo:1,
        pageSize:10,
        active:0,
        activeKey:0,
        formState:{
            id:null,
            devOpsDate:null,
            appealContent:null,
            images:null
        },
        showAppeal:false,
        onBeforeClose(action, done) {
            if (action == 'confirm') {
                return false
            } else {
                return true
            }
        },
    },
    //创建动画
    toggleAnimation: function(angle) {
        var animation = wx.createAnimation({
          timingFunction: "ease"
        })
        this.animation = animation;
        animation.rotate(angle).step()
    },
    invoiceToggle: function() {
        this.pickUp()
        if (this.data.cityShow=='4') {
          this.setData({
            cityShow: '0',
          })
        } else {
          this.toggleAnimation(180)
          this.setData({
            cityShow: '4',
          })
        }
        this.setData({
          invoice_animationData: this.animation.export()
        })
    },
    yearToggle: function() {
        this.pickUp()
        if (this.data.cityShow=='5') {
          this.setData({
            cityShow: '0',
          })
        } else {
          this.toggleAnimation(180)
          this.setData({
            cityShow: '5',
          })
        }
        this.setData({
          year_animationData: this.animation.export()
        })
    },
    monthToggle: function() {
        this.pickUp()
        if (this.data.cityShow=='6') {
          this.setData({
            cityShow: '0',
          })
        } else {
          this.toggleAnimation(180)
          this.setData({
            cityShow: '6',
          })
        }
        this.setData({
          month_animationData: this.animation.export()
        })
    },
    setInvoice:function (e) {
        console.log("setUser-->",e)
       
        let invoiceIndex = e.target.dataset.index; //当前点击的索引
        let invoiceName = e.target.dataset.label;
        let status = e.target.dataset.value;
        this.toggleAnimation(0)
        this.setData({
            cityShow: '0',
            invoiceIndex,
            invoiceName,
            status,
            invoice_animationData: this.animation.export()
        })
        this.getClaimExpensesList()
    },
    setYear:function (e) {
        console.log("setYear-->",e)
       
        let yearIndex = e.target.dataset.index; //当前点击的索引
        let selectYear = e.target.dataset.year;
        this.toggleAnimation(0)
        this.setData({
            cityShow: '0',
            yearIndex,
            selectYear,
            year_animationData: this.animation.export()
        })
        this.getClaimExpensesList()
    },
    setMonth:function (e) {
        console.log("setMonth-->",e)
       
        let monthIndex = e.target.dataset.index; //当前点击的索引
        let selectMonth = e.target.dataset.month;
        this.toggleAnimation(0)
        this.setData({
            cityShow: '0',
            monthIndex,
            selectMonth,
            month_animationData: this.animation.export()
        })
        this.getClaimExpensesList()
    },
    cancelSelect: function(){
        this.pickUp()
        this.setData({
          cityShow: '0'
        })
    },
    pickUp: function(){
        if (this.data.cityShow == '4') {
            this.toggleAnimation(0)
            this.setData({
              invoice_animationData: this.animation.export()
            })
        } else if (this.data.cityShow == '5') {
          this.toggleAnimation(0)
          this.setData({
            year_animationData: this.animation.export()
          })
        } else if (this.data.cityShow == '6') {
          this.toggleAnimation(0)
          this.setData({
            month_animationData: this.animation.export()
          })
        }
    },
    // tab切换
    tabChange(e){
        let that = this
        that.data.pageNo =1
        this.setData({
            active:e.detail.index,
            pageNo:that.data.pageNo
        })
        that.getClaimExpensesList()
    },
    reset() {
        let that = this
        that.setData({
            cityShow: '0',
            invoiceName: '全部',
            selectYear:new Date().getYear()+1900,
            selectMonth:'全部月份',
            invoiceStatus:'',
            pageNo:1,
            pageSize:10,
            invoice_animationData: {},
            year_animationData: {}, 
            month_animationData: {},
        })
    },
    //获取数据字典
    getProjectList(code) {
        let that = this
        lbbTaskService.getDictByCode({
            code: code
        }, function (res) {
            that.setData({
                invoiceList: res.data,
            })
        })
    },
    // 提交申诉
    submitAppeal(e) {
        let that = this
        this.resetMarkInfo()
        that.data.formState.id = e.currentTarget.dataset.id
        that.data.formState.devOpsDate = e.currentTarget.dataset.date
        that.setData({
            formState:that.data.formState,
            showAppeal:true
        })
        
    },
    // 跳转页面 
    navigateToNext(e) {
        wx.navigateTo({
          url: '/pkgCost/pages/expenses-appeal/index',
        })
    },
    //备注
    changeRemark(e) {
        this.data.formState.appealContent = e.detail.value
        this.setData({
            formState: this.data.formState
        })
    },
    //上传图片
    setImgArr(e) {
        let that = this
        let imgArray = e.detail;
        let newUpLoadImg = []
        let oldUpLoadImg = []
        imgArray.forEach(item => {
            if (item.indexOf('http://tmp/') != -1 || item.indexOf('wxfile://tmp') != -1) {
                newUpLoadImg.push(item)
            } else {
                oldUpLoadImg.push(item)
            }
        })
        wx.showLoading({
            title: '正在上传图片',
            mask: true
        })
        util.newUpLoadImg(app.globalData.header, newUpLoadImg, 0, [], function (imgIdArray) {
            wx.hideLoading()
            oldUpLoadImg.push(...imgIdArray)
            that.data.formState.images = oldUpLoadImg
            that.setData({
                formState: that.data.formState
            })
        });
    },
    onClose() {
        let that=this
        that.setData({
            showAppeal:false
        })
    },
    onConfirmAppeal() {
        let that=this
        if (that.data.formState.id && that.data.formState.appealContent ) {
            let params = that.data.formState
            if(params.images=='null') {
                delete params.images
            }
            wx.showLoading({
              title: '加载中',
            })
            lbbTaskService.submitFeeAppeal(params,function(res){
                wx.hideLoading()
                that.onClose()
                that.getClaimExpensesList()
            })
        } else {
            this.openPrompt()
        }
    },
    resetMarkInfo() {
        this.setData({
            formState:{
                id:null,
                devOpsDate:null,
                appealContent:null,
                images:''
            }
        })
    },
    //校验提示
    openPrompt() {
        wx.showToast({
            title: '请检查必填项',
            icon: 'error',
            duration: 2000
        })
    },
    //上拉加载更多
    eventhandle(e) {
        let that = this
        let pagenum = Number(that.data.pageNo)
        if (pagenum >= that.data.TotalPage) {
            that.setData({
                loading: false,
                loaded: true
            })
        } else {
            pagenum = pagenum + 1
            that.data.pageNo = pagenum.toString()
            that.setData({
                loading: true,
                loaded: false,
                pageNo:that.data.pageNo
            })
            that.getClaimExpensesList()
        }
    },
    //获取报销费用
    getClaimExpensesList(){
        let  that = this
        let params = {
            pageNo:that.data.pageNo,
            pageSize: that.data.pageSize,
            userId: that.data.userId,
            status: that.data.active+1,
            year:that.data.selectYear,
        }
        if(that.data.invoiceName!='全部'){
            params['invoiceStatus'] = that.data.status
        }
        if(that.data.selectMonth!='全部月份'){
            params['month'] = that.data.selectMonth
        }
        wx.showLoading({
            title: '加载中',
        })
        lbbTaskService.getDetailFeeList(params,function(res){
            if(that.data.pageNo==1) {
                let lists = [ ...res.data.records]
                if(lists.length==0) {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        expensesList:lists
                    })
                    wx.hideLoading()
                } else {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        expensesList:lists
                    })
                    wx.hideLoading()
                }
            } else {
                let lists = [...that.data.expensesList, ...res.data.records]
                console.log(lists)
                if(lists.length==0) {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        expensesList:lists
                    })
                    wx.hideLoading()
                } else {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        expensesList:lists
                    })
                    wx.hideLoading()
                }
            }
        })
        
     },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this;
        //获取系统的参数，scrollHeight数值,微信必须要设置style:height才能监听滚动事件
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                scrollHeight: parseInt(res.windowHeight)+200
                })
            }
        });
        this.setData({
            userId:options.userId,
            selectMonth:options.month.split('-')[1]
        })
        this.getProjectList('invoice_type')
        this.getClaimExpensesList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})